﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using System;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using IPMS.BLL;
using Resources;
using System.Data;
using SysOperationLayer.DBHandle;
using SysComm;


public partial class SysLogin : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (base.Request.Form["__EVENTTARGET"] == "lbEng")
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            this.Session["Culture"] = "en-US";
        }
        else if (base.Request.Form["__EVENTTARGET"] == "lbTradChi")
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-TW");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-TW");
            this.Session["Culture"] = "zh-TW";
        }
        else if (base.Request.Form["__EVENTTARGET"] == "lbSimpChi")
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-CN");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
            this.Session["Culture"] = "zh-CN";
        }

        if (Session["Culture"] != null)
        {
            if (Session["Culture"].ToString() == "en-US")
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            }
            else if (Session["Culture"].ToString() == "zh-TW")
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-TW");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-TW");
            }
            else if (Session["Culture"].ToString() == "zh-CN")
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("zh-CN");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
            }
        }
        else
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            this.Session["Culture"] = "en-US";

        }
        base.InitializeCulture();
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void LoginButton_Click(object sender, EventArgs e)
    {
        UsersDB userdb = new UsersDB();
        DataRow dr = userdb.checkLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim());

        if (dr != null)
        {
            this.Session[SysUtil.m_str_session_userid] = userdb.GetValue(dr, "Rowno");
            this.Session[SysUtil.m_str_session_username] = userdb.GetValue(dr, "UserName");
            this.Session[SysUtil.m_str_session_loginname] = userdb.GetValue(dr, "LoginName");
            this.Session[SysUtil.m_str_session_usergroupid] = userdb.GetValue(dr, "GroupId");
            this.Session[SysUtil.m_str_session_usergroupname] = userdb.GetValue(dr, "GroupName");
            this.Session[SysUtil.m_str_session_usertype] = userdb.GetValue(dr, "GroupType");
            this.Session[SysUtil.m_str_session_userrole] = SysUtil.m_user_roles[userdb.GetIntValue(dr, "UserRole") - 1];

            if (this.Session[SysUtil.m_str_session_usertype].ToString().ToLower() == SysUtil.m_str_group_type_administrator.ToLower())
            {
                this.Session[SysUtil.m_str_session_issuperuser] = true;
            }
            else
            {
                this.Session[SysUtil.m_str_session_issuperuser] = false;
            }

            this.Session[SysUtil.m_str_session_useralertday] = userdb.GetValue(dr, "AdvanceAlertDay");
            new LogDB(userdb.GetDB()).writeSucLog(SysUtil.m_functiong_login, "IPMS Login Succeed!");
            userdb.CloseDB();
            lblError.Text = "";
            if (this.Session[SysUtil.m_str_session_usertype].ToString().ToLower() == SysUtil.m_str_group_type_operator.ToLower())
            {
                base.Response.Redirect("SysLogs.aspx");
            }
            else
            {
                base.Response.Redirect("OptReminder.aspx");
            }
            
        }
        else
        {
            lblError.Text = "Your login attempt was not successful. Please try again.";

            if (userdb.ErrorMessage != "")
            {
                lblError.Text += "ERROR:" + userdb.ErrorMessage;
            }

            new LogDB(userdb.GetDB()).writeLog(SysUtil.m_functiong_login, "", "", "", "IPMS Login Failure!, Username:" + this.txtUserName.Text, "FAILURE", Request.UserHostAddress.ToString(), DateTime.Now.ToString());
            userdb.CloseDB();
        }
    }
}
